package com.xq.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xq.entity.IndustryChain;
import com.xq.entity.IndustryChainName;
import com.xq.mapper.IndustryChainMapper;
import com.xq.vo.IndustryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.PostConstruct;
import javax.validation.constraints.Max;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class IndustryServiceImpl {

    @Autowired
    private IndustryChainMapper mapper;

    public void init(){
        List<IndustryVo> list = mapper.selectIndustryList();

        List<IndustryVo> rootIndustryList = Lists.newArrayList();
        list.forEach(c->{
            if(c.getPid().intValue()==0){
                rootIndustryList.add(c);
            }
        });

        rootIndustryList.forEach(c->{
            List<IndustryVo> childrenList = getChildren(c.getId(),list);
            c.setChildren(childrenList);
        });

        System.out.println(JSON.toJSONString(rootIndustryList));
    }

    public List<IndustryVo> getChildren(Integer pid,List<IndustryVo> allIndustryList){
        List<IndustryVo> childrenIndustryList = Lists.newArrayList();
        allIndustryList.forEach(c->{
            if(c.getPid().intValue()==pid.intValue()){
                childrenIndustryList.add(c);
            }
        });

        //递归
        for (IndustryVo nav : childrenIndustryList) {
            nav.setChildren(getChildren(nav.getId(),allIndustryList));
        }

        return childrenIndustryList;
    }




}
